<template>
  <div>
    <TopFilter ref="form" :cols="2" :rows="2" :list="list" @filterChange="handleSearch"></TopFilter>
    <!-- <div style="margin-bottom: 20px; text-align: right;">
      <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">查询</el-button>
      <el-button type="primary" size="small" icon="el-icon-refresh-left" @click="handleReset">重置</el-button>
    </div> -->
    <FilterTable
      ref="table"
      height="auto"
      maxHeight="350"
      columnsRef="partAccessAccessInventoryOrderSearch"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      selectionType="single"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="
        rows => {
          this.selectRows = rows;
        }
      "
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button size="small" :disabled="false" @click="$emit('cancel')">取消</el-button>
      <el-button type="primary" size="small" :disabled="false" @click="confirmBtnClick">确定</el-button>
      <!-- <el-button type="primary" @click="closeDrawer(2)">保 存</el-button> -->
    </div>
    <!-- <el-row type="flex" justify="center" style="margin: 15px 0;">
      <el-button size="small" :disabled="false" @click="$emit('cancel')">取消</el-button>
      <el-button type="primary" size="small" :disabled="false" @click="confirmBtnClick">确定</el-button>
    </el-row>-->
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { findAllInventoryInfo, inventoryCheck } from '@/api/part/access/accessInventory';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
export default {
  name: 'access_order_serch',
  mixins: [dictionary],
  props: {
    ptype: String
  },
  data() {
    return {
      list: this.createList(),
      columns: this.createColumns(),
      fetchApi: inventoryCheck,
      fetchParams: { xhrAbort: true },
      selectRows: [],
      serviceAdvisorData: []
    };
  },
  mounted() {
    this.$refs.form.SUBMIT_FORM();
    this.serachService();
  },
  methods: {
    createList() {
      return [
        {
          type: 'SELECT',
          label: '盘点单状态',
          fieldName: 'IS_CONFIRMED',
          initialValue: '10041002',
          itemList: [
            {
              text: '未盘点确认',
              value: '10041002'
            },
            {
              text: '已盘点确认',
              value: '10041001'
            }
          ],
          change: () => {
            console.log('盘点单状态 change');
            this.$refs.form.SUBMIT_FORM();
          }
        },
        {
          type: 'INPUT',
          label: '盘点单号',
          fieldName: 'inventoryNo',
          initialValue: '',
          readonly: false
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'beginDate|endDate',
          label: '盘点日期'
        },
        {
          type: 'SELECT',
          label: '是否抽盘',
          fieldName: 'IS_STOCK_INVENTORY',
          itemList: [
            { text: '是', value: 10041002 },
            { text: '否', value: 10041001 }
          ]
        }
        // ,
        // {
        //   type: 'DATE',
        //   label: '开始日期',
        //   fieldName: 'beginDate',
        //   initialValue: '',
        //   disabled: false
        // },
        // {
        //   type: 'DATE',
        //   label: '结束日期',
        //   fieldName: 'endDate',
        //   initialValue: '',
        //   disabled: false
        // }
      ];
    },
    createColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 100,
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          },
          showOverflowTooltip: true
        },
        {
          title: '盘点单号',
          dataIndex: 'INVENTORY_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '盘点日期',
          dataIndex: 'INVENTORY_DATE',
          width: '180',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true
        },
        {
          title: '经手人',
          dataIndex: 'HANDLER',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.getEmployeeName(props.row.HANDLER)}</span>;
          },
          showOverflowTooltip: true
        },
        {
          title: '锁定人',
          dataIndex: 'LOCK_USER',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.getEmployeeName(props.row.HANDLER)}</span>;
          },
          showOverflowTooltip: true
        },
        {
          title: '盘点类型',
          dataIndex: 'ORDER_STATUS',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          dictItems: this.createDictList(9195),
          filterItems: this.createDictList(9195),
          showOverflowTooltip: true
        },
        {
          title: '操作员',
          dataIndex: 'HANDLERH',
          sorter: true,
          render: props => {
            return <span>{this.getEmployeeName(props.row.HANDLERH)}</span>;
          },
          showOverflowTooltip: true
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          filter: true
        }
        // {
        //   title: '锁定人',
        //   dataIndex: 'LOCK_USER',
        //   sorter: true,
        //   filter: true
        // }
      ];
    },
    // 点击查询按钮
    handleSearch(e) {
      this.fetchParams = Object.assign({ xhrAbort: false }, e, { ptype: this.ptype });
      this.fetchParams = { ...this.fetchParams };
      // this.fetchParams.xhrAbort = true;
      // this.fetchParams = Object.assign(this.fetchParams, ...e, {
      //   xhrAbort: false
      // });
    },
    getEmployeeName(userId) {
      const item = this.serviceAdvisorData.find(x => {
        return x.value == userId;
      });
      return item ? item.text : '';
    },
    async serachService() {
      let res = await getAllUser();
      this.serviceAdvisorData = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
    },
    queryActive(val) {
      console.log('盘点单查询');
      this.fetchParams = Object.assign({}, val);
    },
    confirmBtnClick() {
      const [row] = this.selectRows;
      if (row) {
        const _row = Object.assign({}, row);
        this.$emit('confirm', _row);
      }
    }
  }
};
</script>
